import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.*;

import exceptions.ArgomentoNonValido;



/**
 * @author Antonio Nicora
 */
public class GUI extends JFrame implements WindowListener {
	/**
	 * 
	 */
	private GUI riferimentoGUI;
	private Main logica;
	private static final long serialVersionUID = 1L;
	private JPanel this2;
	private JTextField mail;
	private JTextField nomeutente;
	private JPasswordField password;
	private JButton button1;
	private JButton button2;
	private JRadioButton radioButton1;
	private JRadioButton radioButton2;
	private JRadioButton radioButton3;
	private JLabel label1;
	private JLabel label2;
	private JLabel label3;
	
	private JTextField nome;
	private JTextField cognome;
	private JTextField dataN;
	private JLabel label4;
	private JLabel label5;
	private JLabel label6;
	private JLabel label7;
	private JTextField cf;
	
	
	public GUI(Main logica) {
		riferimentoGUI = this;
		this.logica = logica;
		initComponents();
		logica.inizializza();
	}

	
	
	
	private void initComponents() {
		
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Antonio Nicora
		
		this2 = new JPanel();
		mail = new JTextField();
		nomeutente = new JTextField();
		password = new JPasswordField();
		button1 = new JButton();
		button2 = new JButton();
		radioButton1 = new JRadioButton();
		radioButton2 = new JRadioButton();
		radioButton3 = new JRadioButton();
		label1 = new JLabel();
		label2 = new JLabel();
		label3 = new JLabel();

		//======== this ========
		Container contentPane = getContentPane();

		//======== this2 ========
		{

			
			//---- button1 ----
			button1.setText("Login");

			//---- button2 ----
			button2.setText("Registrazione");

			button2.addActionListener(new RegistrazioneListener());
			
			
			//---- radioButton1 ----
			radioButton1.setText("Registrato");

			//---- radioButton2 ----
			radioButton2.setText("Amministratore");

			//---- radioButton3 ----
			radioButton3.setText("Ospite");

			//---- label1 ----
			label1.setText("MAIL");

			//---- label2 ----
			label2.setText("USERNAME");

			//---- label3 ----
			label3.setText("PASSWORD");

			GroupLayout this2Layout = new GroupLayout(this2);
			this2.setLayout(this2Layout);
			this2Layout.setHorizontalGroup(
				this2Layout.createParallelGroup()
					.addGroup(GroupLayout.Alignment.TRAILING, this2Layout.createSequentialGroup()
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(this2Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
							.addComponent(label3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(label1, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
							.addComponent(label2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.addGap(18, 18, 18)
						.addGroup(this2Layout.createParallelGroup()
							.addComponent(password, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
							.addComponent(mail, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
							.addComponent(nomeutente, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE))
						.addGap(85, 85, 85))
					.addGroup(GroupLayout.Alignment.TRAILING, this2Layout.createSequentialGroup()
						.addGap(0, 28, Short.MAX_VALUE)
						.addGroup(this2Layout.createParallelGroup()
							.addGroup(GroupLayout.Alignment.TRAILING, this2Layout.createSequentialGroup()
								.addComponent(button1)
								.addGap(65, 65, 65)
								.addComponent(button2)
								.addGap(119, 119, 119))
							.addGroup(GroupLayout.Alignment.TRAILING, this2Layout.createSequentialGroup()
								.addComponent(radioButton1)
								.addGap(58, 58, 58)
								.addComponent(radioButton2)
								.addGap(42, 42, 42)
								.addComponent(radioButton3)
								.addGap(96, 96, 96))))
			);
			this2Layout.setVerticalGroup(
				this2Layout.createParallelGroup()
					.addGroup(this2Layout.createSequentialGroup()
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(this2Layout.createParallelGroup()
							.addComponent(label1, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
							.addComponent(mail, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE))
						.addGap(18, 18, 18)
						.addGroup(this2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
							.addComponent(label2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(nomeutente, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE))
						.addGap(18, 18, 18)
						.addGroup(this2Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
							.addComponent(label3)
							.addComponent(password, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE))
						.addGap(40, 40, 40)
						.addGroup(this2Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
							.addComponent(radioButton1)
							.addGroup(this2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(radioButton2)
								.addComponent(radioButton3)))
						.addGap(21, 21, 21)
						.addGroup(this2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
							.addComponent(button2)
							.addComponent(button1))
						.addGap(94, 94, 94))
			);
		}

		GroupLayout contentPaneLayout = new GroupLayout(contentPane);
		contentPane.setLayout(contentPaneLayout);
		contentPaneLayout.setHorizontalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
					.addContainerGap(341, Short.MAX_VALUE)
					.addComponent(this2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		contentPaneLayout.setVerticalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
					.addContainerGap(204, Short.MAX_VALUE)
					.addComponent(this2, GroupLayout.PREFERRED_SIZE, 254, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		pack();
		setLocationRelativeTo(getOwner());
		setVisible(true);
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}
	
	
	public void registrazione(){
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Antonio Nicora
		this2 = new JPanel();
		mail = new JTextField();
		nomeutente = new JTextField();
		password = new JPasswordField();
		button2 = new JButton();
		label1 = new JLabel();
		label2 = new JLabel();
		label3 = new JLabel();
		nome = new JTextField();
		cognome = new JTextField();
		dataN = new JTextField();
		label4 = new JLabel();
		label5 = new JLabel();
		label6 = new JLabel();
		label7 = new JLabel();
		cf = new JTextField();

		//======== this ========
		Container contentPane = getContentPane();

		//======== this2 ========
		{

			
			//---- button2 ----
			button2.setText("Registrati");
			
			button2.addActionListener(new RegistratiListener());

			//---- label1 ----
			label1.setText("MAIL");

			//---- label2 ----
			label2.setText("USERNAME");

			//---- label3 ----
			label3.setText("PASSWORD");

			//---- label4 ----
			label4.setText("NOME");

			//---- label5 ----
			label5.setText("COGNOME");

			//---- label6 ----
			label6.setText("DATA DI NASCITA");

			//---- label7 ----
			label7.setText("CF");

			GroupLayout this2Layout = new GroupLayout(this2);
			this2.setLayout(this2Layout);
			this2Layout.setHorizontalGroup(
				this2Layout.createParallelGroup()
					.addGroup(this2Layout.createSequentialGroup()
						.addGap(52, 52, 52)
						.addGroup(this2Layout.createParallelGroup()
							.addComponent(label2)
							.addComponent(label1, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
							.addComponent(label3)
							.addComponent(label4, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
						.addGap(32, 32, 32)
						.addGroup(this2Layout.createParallelGroup()
							.addComponent(nomeutente, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
							.addComponent(mail, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
							.addComponent(password, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
							.addComponent(nome, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
						.addGroup(this2Layout.createParallelGroup()
							.addGroup(GroupLayout.Alignment.TRAILING, this2Layout.createSequentialGroup()
								.addGroup(this2Layout.createParallelGroup()
									.addComponent(label6)
									.addComponent(label5)
									.addComponent(label7))
								.addGroup(this2Layout.createParallelGroup()
									.addGroup(this2Layout.createSequentialGroup()
										.addGap(40, 40, 40)
										.addComponent(cognome, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE))
									.addComponent(cf, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)))
							.addComponent(dataN, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE))
						.addContainerGap())
					.addGroup(GroupLayout.Alignment.TRAILING, this2Layout.createSequentialGroup()
						.addContainerGap(318, Short.MAX_VALUE)
						.addComponent(button2)
						.addGap(237, 237, 237))
			);
			this2Layout.setVerticalGroup(
				this2Layout.createParallelGroup()
					.addGroup(this2Layout.createSequentialGroup()
						.addGap(122, 122, 122)
						.addGroup(this2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
							.addComponent(label1, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
							.addComponent(mail, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
							.addComponent(cognome, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
							.addComponent(label5))
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
						.addGroup(this2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
							.addComponent(label2)
							.addComponent(nomeutente, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
							.addComponent(dataN, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
							.addComponent(label6))
						.addGap(30, 30, 30)
						.addGroup(this2Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
							.addGroup(this2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(label3)
								.addComponent(password, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
								.addComponent(label7))
							.addComponent(cf, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE))
						.addGap(31, 31, 31)
						.addGroup(this2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
							.addComponent(label4, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
							.addComponent(nome, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGap(36, 36, 36)
						.addComponent(button2)
						.addGap(66, 66, 66))
			);
		}

		GroupLayout contentPaneLayout = new GroupLayout(contentPane);
		contentPane.setLayout(contentPaneLayout);
		contentPaneLayout.setHorizontalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(contentPaneLayout.createParallelGroup()
					.addGroup(contentPaneLayout.createSequentialGroup()
						.addGap(0, 97, Short.MAX_VALUE)
						.addComponent(this2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGap(0, 98, Short.MAX_VALUE)))
				.addGap(0, 829, Short.MAX_VALUE)
		);
		contentPaneLayout.setVerticalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(contentPaneLayout.createParallelGroup()
					.addGroup(contentPaneLayout.createSequentialGroup()
						.addGap(0, 46, Short.MAX_VALUE)
						.addComponent(this2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGap(0, 47, Short.MAX_VALUE)))
				.addGap(0, 529, Short.MAX_VALUE)
		);
		pack();
		setLocationRelativeTo(getOwner());
		setVisible(true);
	
	}
	
	//Listener per passare al pannello di Registrazione da button "Registrazione"
	private class RegistrazioneListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			
		   setVisible(false);
		   riferimentoGUI.remove(this2);
		   riferimentoGUI.validate();
		   riferimentoGUI.repaint();
		   registrazione();
			
			
		}
		
	}
	
	
	//Listener per effettuare l'effettiva registrazione dell'utente da button "Registrati"
	private class RegistratiListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			
			try{
			  String mailL = mail.getText();
			  String nomeutenteL = nomeutente.getText();
			  String passwordL = new String ( password.getPassword());
			  String nomeL = nome.getText();
			  String cognomeL = cognome.getText();
			  String dataNL = dataN.getText();
			  String cfL = cf.getText();
			
			  try {
				 
				if(logica.registrazioneUtente(mailL, nomeutenteL, passwordL, nomeL, cognomeL, dataNL, cfL) == true){
					JOptionPane.showMessageDialog(null, "Registrazione effettuata", "Registrazione utente", JOptionPane.INFORMATION_MESSAGE); 
					  
					  
				  }else{
					  JOptionPane.showMessageDialog(null, "errore di registrazione", "Registrazione utente", JOptionPane.ERROR_MESSAGE);
				  }
				
				
			} catch (ArgomentoNonValido e1) {
				
				e1.printStackTrace();
			}
			  
			  
			}catch(NullPointerException eccezione){
				JOptionPane.showMessageDialog(null, "Errore - dati inseriti non validi", "Registrazione utente", JOptionPane.ERROR_MESSAGE);
			}
		 
			
			
		}
		
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Antonio Nicora
	
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}

